package com.lovo.project.servlet.user;

import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.UserDO;
import com.lovo.project.service.UserService;
import com.lovo.project.service.impl.UserServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 用户注册servlet
 * @author: luo
 * @date: 2020/11/4 16:49
 */
@WebServlet(name = "RegisterUserServlet",urlPatterns = "/user/register")
public class RegisterUserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("userPhone");
        String email = request.getParameter("userEmail");
        String realName = request.getParameter("userRealName");
        String status = "申请中";
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("userPwd");
        PrintWriter out = response.getWriter();
        UserDO userDO = new UserDO();
        userDO.setUserName(userName);
        userDO.setUserPwd(passWord);
        userDO.setUserPhone(phone);
        userDO.setUserEmail(email);
        userDO.setUserRealName(realName);
        userDO.setUserStatus(status);


        try {
            userService.register(userDO);
            out.print(ResultVO.success().toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(UserCodeEnum.USER_UNKNOWN_ERROR).toJson());
        }finally {
            out.close();
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
